package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.CartListBean;
import com.mz.ozy.bean.PopularBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by administrator on 2018/11/21.
 */
public class PopularAdapter extends BaseQuickAdapter<PopularBean.PageBean.PageItemsBean, BaseViewHolder> {


    public PopularAdapter(@Nullable List<PopularBean.PageBean.PageItemsBean> data) {
        super(R.layout.item_poplar_food, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, PopularBean.PageBean.PageItemsBean item) {
        ImageView ivGoodsPic = helper.getView(R.id.iv_goods_banner);
        String imgUrl = "";
        if (!TextUtils.isEmpty(item.getImg_zt())) {
            final String[] img_arr = item.getImg_zt().trim().split(",");
            if (img_arr.length > 0) {
                imgUrl = img_arr[0];
            }

        }
        GlideUtils.loadRoundImg(mContext, ivGoodsPic, imgUrl, R.mipmap.default_foods_icon, 4);
        helper.setText(R.id.tv_goods_name, item.getTradename());
        helper.setText(R.id.tv_benifit_kind, item.getDescribed());
        if (!TextUtils.isEmpty(item.getWeight())) {
            helper.setText(R.id.tv_unit_price, item.getWeight() + "g");
        } else {
            helper.setText(R.id.tv_unit_price, "");
        }
        helper.setText(R.id.tv_goods_price, getPrice(item));
        helper.setText(R.id.tv_exchange, item.getName());

        if (TextUtils.isEmpty(item.getName())) {
            helper.setGone(R.id.tv_exchange, false);
        } else {
            helper.setGone(R.id.tv_exchange, true);
        }
        helper.addOnClickListener(R.id.ll_container);
        helper.addOnClickListener(R.id.tv_goods_pic);
        if (helper.getAdapterPosition() / 2 == 0) {
            helper.setVisible(R.id.head_view_divider, true);
        } else {
            helper.setGone(R.id.head_view_divider, false);
        }
        if (helper.getAdapterPosition() >= getData().size() - 2) {
            helper.setVisible(R.id.end_view_divider, true);
        } else {
            helper.setGone(R.id.end_view_divider, false);
        }
    }


    private String getPrice(PopularBean.PageBean.PageItemsBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += "¥";
            price += item.getPrice();
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "积分";
            } else {
                price += item.getMarket_price();
                price += "积分";
            }
        }

        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        if (item.getProducttype() == 1) {
            return price.replace("¥", "") + "欧花";
        }


        return price;
    }
}
